package com.app.service.impl;

import com.app.model.Article;
import com.app.model.Banner;
import com.app.mapper.BannerMapper;
import com.app.page.CommonPage;
import com.app.request.PageParamRequest;
import com.app.service.IBannerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 轮播图管理	 服务实现类
 * </p>
 *
 * @author ghoul
 * @since 2024-12-27
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Override
    public PageInfo<Banner> getAdminList(Banner request, PageParamRequest pageParamRequest) {
        Page<Banner> bannerPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        if (request.getStatus() != null) {
            queryWrapper.eq(Banner::getStatus, request.getStatus());
        }
        queryWrapper.eq(Banner::getIsDel, false);
        queryWrapper.orderByDesc(Banner::getSort);
        List<Banner> list = list(queryWrapper);
        return CommonPage.copyPageInfo(bannerPage, list);
    }

    @Override
    public List<Banner> getSmallList() {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getIsDel, false);
        queryWrapper.eq(Banner::getStatus, false);
        queryWrapper.orderByDesc(Banner::getSort);
        return list(queryWrapper);
    }

    @Override
    public boolean create(Banner banner) {
        banner.setCreateTime(LocalDateTime.now());
        return save(banner);
    }

    @Override
    public boolean deleteById(Integer id) {
        Banner banner = getById(id);
        if (banner == null) {
            return false;
        }
        banner.setIsDel(true);
        banner.setUpdateTime(LocalDateTime.now());
        return updateById(banner);
    }

    @Override
    public Banner getDetail(Integer id) {
        return getById(id);
    }

    @Override
    public boolean updateStatus(Integer id, boolean status) {
        Banner banner = getById(id);
        if (banner == null) {
            return false;
        }
        banner.setStatus(status);
        banner.setUpdateTime(LocalDateTime.now());
        return updateById(banner);
    }
}
